//
//  BK_OD_Price_Cell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/28.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_Price_Cell.h"
#import "BK_OD_Color.h"
#import "BK_Calendar_Tool.h"
#import "BK_OD_Tool.h"
NSInteger const order_maxTextLenght = 9;



/**
 选择出价
 */
@implementation BK_OD_Price_Cell
{
    UIView *_BG;
//    UITextField *_textField_money;
    UIButton *_sub;
    UIButton *_add;
    UILabel *_priceTitle;
    NSString *_currentPrice;
    UIView *_topLine;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.separatorInset = UIEdgeInsetsMake(0, 50, 0, ApplicationBackGauge);
    
    _BG = [[UIView alloc] init];
    _BG.backgroundColor = kOrderLightGrey;
    [self.contentView addSubview:_BG];
    
//    _price = [[UITextField alloc] init];
//    _price.borderStyle = UITextBorderStyleNone;
//    _price.delegate = self;
//    _price.font = UIFontCustom(15);
//    _price.textColor = kOrderPriceRed;
//    _price.textAlignment = NSTextAlignmentCenter;
//    _price.returnKeyType = UIReturnKeyDone;
//    _price.keyboardType = UIKeyboardTypeDecimalPad;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
//    [self.contentView addSubview:_price];
    
    _textField_money = [UITextField new];
    _textField_money.backgroundColor=kOrderPriceBGGrey;
//    _textField_money.backgroundColor=RGBACOLOR(249, 250, 251, 1);
    _textField_money.clearButtonMode = UITextFieldViewModeNever;
    _textField_money.returnKeyType = UIReturnKeyDone;
    _textField_money.keyboardType = UIKeyboardTypeNumberPad;
    [_textField_money addTarget:self action:@selector(BK_text_ValueChanged:) forControlEvents:UIControlEventEditingChanged];
    [_textField_money setText:@"0"];
    [_textField_money sizeToFit];
    if (ScreenHeight>600) {
        [_textField_money setFont:[UIFont systemFontOfSize:18.0f]];
    }else{
        [_textField_money setFont:[UIFont systemFontOfSize:16.0f]];
    }
    
    [_textField_money setTextAlignment:NSTextAlignmentCenter];
    [_textField_money setTextColor:kOrderPriceRed];
    [self.contentView addSubview:_textField_money];
    _textField_money.delegate =self;
    _textField_money.inputView = ({
        WLDecimalKeyboard *inputView = [[WLDecimalKeyboard alloc] init];
        inputView.keyboard_Delegate = self;
        inputView;
    });
    [_textField_money reloadInputViews];
    [self.contentView addSubview:_textField_money];
    
    
    
    
    _sub = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sub addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    [_sub setTitleColor:kOrderDrakGrey forState:UIControlStateNormal];
    _sub.titleLabel.font = UIFontCustom(26);
    [self.contentView addSubview:_sub];
    
    _add = [UIButton buttonWithType:UIButtonTypeCustom];
    [_add addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [_add setTitleColor:kOrderDrakGrey forState:UIControlStateNormal];
    _add.titleLabel.font = UIFontCustom(25);
    [self.contentView addSubview:_add];
    
    /*
     * 出价/预购价
     */
    _priceTitle = [[UILabel alloc] init];
    _priceTitle.textColor=kOrderBlack1;
    _priceTitle.font = UIFontCustom(15);
    [self.contentView addSubview:_priceTitle];
    
    [self makeConstraints];
    
}

- (void)makeConstraints
{
    [_BG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
       
        if(ScreenHeight>600) {
            make.width.mas_equalTo(140);
            make.centerX.mas_equalTo(self.contentView).offset(0);
        }else{
            make.width.mas_equalTo(110);
            make.centerX.mas_equalTo(self.contentView).offset(0);
        }
        
        make.height.mas_equalTo(30);
    }];
    
    [_textField_money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(_BG);
    }];
    
    [_sub mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(_BG.mas_left).offset(-5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_add mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(_BG.mas_right).offset(5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_priceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(ApplicationBackGauge);
    }];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (!_topLine) {
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 1)];
        _topLine.backgroundColor = kOrderWhite;
        [self addSubview:_topLine];
    }
}

- (void)subAction:(UIButton *)sender
{
    double price = [[_textField_money.text substringFromIndex:1] doubleValue];
    price -= 1;
    
    _textField_money.text = [NSString stringWithFormat:@"￥%.2f",price];
    
    [self textFieldDidEndEditing:_textField_money];
}

- (void)addAction:(UIButton *)sender
{
    double price = [[_textField_money.text substringFromIndex:1] doubleValue];
    price += 1;
    
    _textField_money.text = [NSString stringWithFormat:@"￥%.2f",price];
    
    [self textFieldDidEndEditing:_textField_money];
}

- (void)fillData:(id)data
{
    if (!data) {
        return;
    }
    double priceN;
    BKODDeliveryDate *deliveryDate = data[@"delivery"];
    BKODStrongestMatch *strongMatch = data[@"strongMatch"];
    if ([deliveryDate.thbuyflag isEqualToString:@"0"]) {
        _add.hidden = YES;
        _sub.hidden = YES;
        
        /*
         * 预购价
         */
        _priceTitle.text = kOrderLocalizeString(@"BKOrderPrepaidPrice");//预购价
        priceN = [[deliveryDate.avgpriceStr substringFromIndex:1] doubleValue];
        
        [_textField_money setEnabled:NO];
    }
    else if ([deliveryDate.thbuyflag isEqualToString:@"1"]){
        _sub.hidden = NO;
        _add.hidden = NO;
        
        [_sub setImage:[UIImage imageNamed:@"BKOrder_sub_black"] forState:UIControlStateNormal];
        [_add setImage:[UIImage imageNamed:@"BKOrder_add_black"] forState:UIControlStateNormal];
        
        /*
         * 出价
         */
        _priceTitle.text = kOrderLocalizeString(@"BKOrderDDPrice");
        priceN = [strongMatch.price_v2 doubleValue];
        
        [_textField_money setEnabled:YES];
    }
    
    
    _currentPrice = [NSString stringWithFormat:@"￥%.2f",priceN];
    _textField_money.text = _currentPrice;
}

+(CGFloat)computeHeight:(id)data
{
//    return 72;
    return 66;
}

#pragma mark  textField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (NSEqualRanges(range, NSMakeRange(0, 1))) {
        return NO;
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_textField_money.text isEqualToString:@"￥"]) {
        kOrderPromptMsg(@"出价不能为空哦");
        _textField_money.text = _currentPrice;
        return;
    }
    
    if ([[_textField_money.text substringFromIndex:1] doubleValue] <= 0) {
        kOrderPromptMsg(@"出价太少了哦");
        _textField_money.text = _currentPrice;
        return;
    }
    _currentPrice = _textField_money.text;
    
    /*
     * 调 出价接口
     */
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODSelectPriceCellDelegate)]) {
        double price = [[textField.text substringFromIndex:1] doubleValue];
        NSString *returnPrice = [NSString stringWithFormat:@"%.2lf", price];
        [self.delegate ODSelectPriceCell:self didSelectPriceChanged:returnPrice];
    }
}

- (void)textFieldDidChange:(NSNotification *)notification
{
    if (_textField_money.text.length > order_maxTextLenght) {
        kOrderPromptMsg(@"出价位数太多了");
        _textField_money.text = [_textField_money.text substringToIndex:order_maxTextLenght];
    }
}

#pragma mark=========监听变化
- (void)BK_text_ValueChanged:(UITextField *)textField{
    if (_textField_money.text.length>9) {
        _textField_money.text=[_textField_money.text substringToIndex:9];
    }

}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark=======================【textFeild_MoneyDelegate】=======================
#pragma mark-输入小数点
- (void)KeyboardDelegate_clicked_Dot{
    if ([BK_Calendar_Tool countChar:_textField_money.text cchar:@"."]>1){
        _textField_money.text=[_textField_money.text substringToIndex:[_textField_money.text length] - 1];
    }else{
        if ([self get_textFeild_Money].length==1&&[[self get_textFeild_Money] isEqualToString:@"."]){
            _textField_money.text=@"￥0.";
        }
    }
}
-(NSString *)get_textFeild_Money{
    if (_textField_money.text){
        if([_textField_money.text rangeOfString:@"￥"].location !=NSNotFound){
            return  [_textField_money.text substringFromIndex:1];
        }else{
            return _textField_money.text;
        }
    }else{
        return @"0.00";
    }
}
#pragma mark-点击删除
- (void)KeyboardDelegate_clicked_Delete{
    if (_textField_money.text.length<1){
        _textField_money.text=@"￥";
    }
}
#pragma mark-点击收回键盘
- (void)KeyboardDelegate_clicked_Dismiss{
//    if (!self.marr_Nowlist||self.marr_Nowlist.count==0){
//    }else{
//        _textField_money.text=_str_last_money;// 回传价格
//        [self relayOutMontAndMoneyWidth];
//        _view_down_one.textField_mont.text=_str_last_mont;  // 回传数量
//    }
}
#pragma mark-点击确定
- (void)KeyboardDelegate_clicked_Sure{
    
    /*
     * 调 出价接口
     */
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODSelectPriceCellDelegate)]) {
        double price = [[_textField_money.text substringFromIndex:1] doubleValue];
        NSString *returnPrice = [NSString stringWithFormat:@"%.2lf", price];
        [self.delegate ODSelectPriceCell:self didSelectPriceChanged:returnPrice];
    }
}

#pragma mark-输入字符
- (void)KeyboardDelegate_clicked_Decimal:(int)decimal{
    
    if (_textField_money.text.length>3){
        if ([_textField_money.text rangeOfString:@"."].location!=NSNotFound){//有小数点
            NSRange ran = [_textField_money.text rangeOfString:@"."];
            if (_textField_money.text.length - ran.location <= 2)
            {
            }else{
                _textField_money.text=[_textField_money.text substringToIndex:ran.location+2+1];
            }
        }
    }else{
        if ([self get_textFeild_Money].length==2&&[[[self get_textFeild_Money] substringToIndex:1] isEqualToString:@"0"]&&[[self get_textFeild_Money]  rangeOfString:@"."].location ==NSNotFound ){//09->0
            _textField_money.text=[_textField_money.text substringToIndex:2];
        }else{
            if ([BK_Calendar_Tool countChar:_textField_money.text cchar:@"0"]>1){
                _textField_money.text=[_textField_money.text substringToIndex:[_textField_money.text length] - 1];
            }else if([[[self get_textFeild_Money] substringToIndex:0] isEqualToString:@"0"]&&[[[self get_textFeild_Money] substringToIndex:1] rangeOfString:@"."].location==NSNotFound ){
                _textField_money.text=[_textField_money.text substringToIndex:[_textField_money.text length] - 1];
            }
        }
    }
}
@end



































